# 控制输入参数的类型，必须输入一个整数代表 jdk 版本
param (
	[int]$v = $(throw "Parameter missing: -v version")
)

# ps1 脚本变量赋值方法
[string]$oldVersionPath = [Environment]::GetEnvironmentVariable('JAVA_HOME')
Write-Host "JAVA_HOME: $oldVersionPath"

# ps1 默认不支持 ANSI Escape Code
[string]$RED = "\33[1;31m"
[string]$GREEN = "\33[1;32m"
[string]$NONE = "\33[0m"

# ps1 脚本数组
$supportVersions = 8,11,17,21

# ps1 脚本 if else 语法 以及数组包含逻辑表达式
if ( $supportVersions -contains $v )
{
	Write-Host "Supported JDK Versions: $supportVersions"
	Write-Host "Your Choice: $v"
	# User 范围修改环境变量
	[Environment]::SetEnvironmentVariable('JAVA_HOME', "E:\JAVA\jdks\jdk$v", 'User')
	
	# Write-Host 原生支持修改颜色
	Write-Host "JAVA_HOME is updated " -NoNewLine
	Write-Host "successfully" -ForeGroundColor:Green
	# 将当前终端的相关环境变量修改了，就不用重新加载界面了
	$Env:JAVA_HOME = [System.Environment]::GetEnvironmentVariable("JAVA_HOME","User")
	$Env:Path = [System.Environment]::GetEnvironmentVariable("Path","User")
} 
else {
	Write-Host "Supported JDK Versions: $supportVersions, please make sure your input is correct."
	Write-Host "JAVA_HOME is updated " -NoNewLine
	Write-Host "unsuccessfully" -ForeGroundColor:Red
}
